<template>
	<jk-dialog
		v-model="showDialog"
		width="800px"
		title="分配锭组"
		append-to-body
		:show-confirm-btn="!readonly"
		:confirm-btn-loading="confirmBtnLoading"
		@on-visible-change="onVisibleChangeOfSetSpinGroupDialog"
		@on-confirm="onConfirmBtnOfSetSpinGroupDialog"
		@on-cancel="onVisibleChangeOfSetSpinGroupDialog(false)"
	>
		<el-form ref="deleteEmpForm" :model="ruleForm" :rules="rules" label-width="90px" :show-message="false">
			<el-row class="flex-start-center-wrap">
				<el-col :span="12">
					<el-form-item class="margin-bottom-10" label="人员">
						<span class="read-only-2">{{ ruleForm.empItem.empName }}</span>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item class="margin-bottom-10" label="岗位">
						<span class="read-only-2">{{ ruleForm.postItem.postName }}</span>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item class="margin-bottom-10" label="机台">
						<span class="read-only-2">{{ ruleForm.machineItem.machineName }}</span>
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item class="margin-bottom-10" label="锭组列表">
						<div class="flex-between-center margin-bottom-4">
							<el-button size="mini" type="primary" @click="onAllSelectBtn">全选</el-button>
							<el-button size="mini" type="default">
								<span>已选</span>
								<span style="font-size: 13px;margin-left: 2px;">{{ checkIds ? checkIds.length : 0 }}</span>
							</el-button>
						</div>
						<div class="spin-group-container">
							<el-checkbox-group v-model="checkIds" :disabled="readonly" size="small" class="flex-start-center-wrap">
								<div
									v-for="(item, index) in machineDetailData.spinGroupList"
									:key="index"
									class="spin-group-item"
								>
									<el-checkbox
										class="width-100-percent"
										:label="item.id"
										border
									>{{ item.groupIndex }}</el-checkbox>
								</div>
							</el-checkbox-group>
							<div v-if="!machineDetailData.spinGroupList || !machineDetailData.spinGroupList.length" class="text-center width-100-percent" style="font-size: 13px;">暂无数据</div>
						</div>
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
	</jk-dialog>
</template>

<script>
    import { machinemanagerMachineGet } from '../../../../api/machine/machineArchives';
    import { workforcemanagementScheduleEmpMachineUpdate } from '../../../../api/schedule/scheduling';

    export default {
        props: {
            readonly: { // 只读
                type: Boolean,
                default: true
            },
            dialogState: {
                type: Boolean,
                default: false
            },
            selectMachineItem: {
                type: Object,
                default() {
                    return {
                        empItem: {},
                        postItem: {},
                        machineItem: {}
                    };
                }
            }
        },
        data() {
            return {
                showDialog: false,
                checkIds: [],
                ruleForm: {
                    empItem: {},
                    postItem: {},
                    machineItem: {}
                },
                rules: {},
                machineDetailData: {},
                confirmBtnLoading: false,
                setSpinGroupDialogState: false
            };
        },
        methods: {
            onAllSelectBtn() {
                this.checkIds = this.machineDetailData.spinGroupList.map(x => x.id);
            },
            onVisibleChangeOfSetSpinGroupDialog(e) {
                if (!e) {
                    this.checkIds = [];
                }
                this.$emit('on-visible-change', e);
            },
            getMachineDetailRequest() {
                return machinemanagerMachineGet({
                    id: this.selectMachineItem.machineItem.machineId
                }).then(res => {
                    if (res.data.status === 200) {
                        this.machineDetailData = res.data.res || [];
                    }
                });
            },
            onConfirmBtnOfSetSpinGroupDialog() {
                const spinGroupList = this.machineDetailData.spinGroupList.filter(x => this.checkIds.includes(x.id));
                this.confirmBtnLoading = true;
                workforcemanagementScheduleEmpMachineUpdate({
                    id: this.selectMachineItem.machineItem.id,
                    machineId: this.ruleForm.machineItem.machineId,
                    machineCode: this.ruleForm.machineItem.machineCode,
                    machineName: this.ruleForm.machineItem.machineName,
                    spinGroupList: spinGroupList
                }).then(res => {
                    this.confirmBtnLoading = false;
                    if (res.data.status === 200) {
                        this.$emit('on-confirm');
                    }
                });
            }
        },
        watch: {
            dialogState(newVal) {
                this.checkIds = this.selectMachineItem.machineItem.spinGroupList.map(x => x.id);
                this.ruleForm = JSON.parse(JSON.stringify(this.selectMachineItem));
                this.showDialog = newVal;
                this.getMachineDetailRequest();
            }
        }
    };
</script>
<style scoped lang="scss">
    .spin-group-container {
        background: #f5f7fa;
        padding: 10px;
        border-radius: 4px;
        border: 1px solid #e8eaec;
        .spin-group-item {
            width: calc(100% / 5);
            margin: 0!important;
            background: #fff;
            padding: 2px;
            display: flex;
            align-items: center;
        }
    }
</style>
